﻿// -----------------------------------------------------------------------
// <copyright file="SettingsForm.cs" company="Abyssalsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Metal.Config
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using Metal.Config.ExtendedControls;
    using System.Reflection;
    using System.Xml;

    /// <summary>
    /// The settings window
    /// </summary>
    public partial class SettingsForm : Form
    {
        private bool restartRequired;

        /// <summary>
        /// Initializes a new instance of the SettingsForm class.
        /// </summary>
        /// <param name="showTab">The tab to show when the settings form is shown</param>
        public SettingsForm(ShowTab showTab)
        {
            this.InitializeComponent();

            this.tabControl1.SelectTab(Convert.ToInt32(showTab));

            this.LoadLanguagesCombo();

            this.LoadAvailableThemes();
        }

        /// <summary>
        /// Show specific tab content
        /// </summary>
        public enum ShowTab
        {
            /// <summary>Show General settings tab</summary>
            GeneralSettings = 0,

            /// <summary>Show Playlist tab</summary>
            Playlist = 1,

            /// <summary>Show Themes tab</summary>
            Themes = 2,

            /// <summary>Show Advanced tab</summary>
            Advanced = 3,

            /// <summary>Show Intelligent playback tab</summary>
            IntelligentPlayback = 4,

            /// <summary>Show Audio settings tab</summary>
            AudioSettings = 5,

            /// <summary>Show Video settings tab</summary>
            VideoSettings = 6
        }

        /// <summary>
        /// Saves settings
        /// </summary>
        /// <param name="sender">Object sender</param>
        /// <param name="e">Event args</param>
        private void SaveButton_Click(object sender, EventArgs e)
        {
            this.optionAllowMultipleInstances.SaveSetting();
            this.optionVisualisationStyle.SaveSetting();

            this.optionPlayRecursively.SaveSetting();
            this.optionPlayRandom.SaveSetting();
            this.optionPlaySmart.SaveSetting();

            this.optionLanguage.SaveSetting();
            this.optionThemes.SaveSetting();
            this.optionTransparency.SaveSetting();

            this.optionAudioAssociation.SaveSetting();
            this.optionVideoAssociation.SaveSetting();

            if (optionAudioAssociation.Checked)
            {
                XmlDocument fileAssociation = new XmlDocument();
                fileAssociation.Load(Config.Paths.UserFolder + "FileExtensions.xml");
                XmlNodeList extensions = fileAssociation.SelectNodes("//extension");

                foreach (XmlNode extension in extensions)
                {
                    if (extension.SelectSingleNode("//associate").InnerText == "true")
                    {
                        string ext = "." + extension.FirstChild.Name;
                        string description = extension.FirstChild.InnerText;
                        string iconPath = extension.ChildNodes[1].InnerText;
                        Config.FileAssociation.AssociateExtension(ext, description, iconPath);
                    }
                }
                Config.FileAssociation.NotifyChanges();
            }

            // Reload settings
            Settings.Instance.LoadSettings();
            this.Close();

            if (restartRequired)
            {
                Controller.Accesibility.RestartApplication();
            }
        }

        /// <summary>
        /// Cancel changes
        /// </summary>
        /// <param name="sender">Object sender</param>
        /// <param name="e">Event args</param>
        private void CancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Update graphic equalizer style in real time (no need to save to see the changes)
        /// </summary>
        /// <param name="sender">Object sender</param>
        /// <param name="e">Event args</param>
        private void OptionVisualisationStyle_SelectedIndexChanged(object sender, EventArgs e)
        {
            Core.Player.Settings.Instance.GraphicEqualizerStyle = this.optionVisualisationStyle.SelectedIndex;
        }

        /// <summary>
        /// Load languages combo
        /// </summary>
        private void LoadLanguagesCombo()
        {
            string[] files = System.IO.Directory.GetFiles(Metal.Config.Paths.LanguageFolder);

            this.optionLanguage.Items.Clear();
            foreach (string file in files)
            {
                this.optionLanguage.Items.Add(System.IO.Path.GetFileNameWithoutExtension(file));
            }

            this.optionLanguage.Text = Settings.Instance.GetValue("LastSettings.Language", "English");
        }

        /// <summary>
        /// Load available themes into the list
        /// </summary>
        private void LoadAvailableThemes()
        {
            string[] folders = System.IO.Directory.GetDirectories(Metal.Config.Paths.ThemesFolder);

            this.optionThemes.Items.Clear();
            foreach (string folder in folders)
            {
                string dirName = new System.IO.DirectoryInfo(folder).Name;
                this.optionThemes.Items.Add(dirName);
            }

            string previewImage = Metal.Config.Paths.ThemesFolder + Settings.Instance.GetValue("LastSettings.Themes", "BlackMetal") + "\\preview.png";
            if (System.IO.File.Exists(previewImage))
            {
                this.themePreview.Image = Image.FromFile(previewImage);
            }
        }

        private void optionThemes_SelectedIndexChanged(object sender, EventArgs e)
        {
            string previewImage = Metal.Config.Paths.ThemesFolder + this.optionThemes.SelectedItem.ToString() + "\\preview.png";
            if (System.IO.File.Exists(previewImage))
            {
                this.themePreview.Image = Image.FromFile(previewImage);
            }
        }

        private void optionTransparency_Scroll(object sender, EventArgs e)
        {
            Assembly currentAssembly = Assembly.GetExecutingAssembly();
            foreach (Form form in Application.OpenForms)
            {
                if (form.Name != this.Name)
                {
                    int transparency = this.optionTransparency.Value;
                    float value = (float)transparency / 100;
                    form.Opacity = value;
                }
            }
        }

        private void optionThemes_Click(object sender, EventArgs e)
        {
            restartRequired = true;
        }
    }
}
